
<script type="text/javascript">
	$(document).ready(function(){
		$('#petition').dataTable();
	});
	function printCheque(id){
		var val = [];
		var count=0;
		
		$(':checkbox:checked').each(function(i){
		  val[i] = $(this).val();
		  count++;
		  //alert(val[i]);
		});

		if (count == 0) {
			alert ("กรุณาเลือกฎีกาที่ต้องการพิมพ์ด้วยครับ");
		} else {
			$('<div>').dialog2({
				autoOpen: true,
				title: "พิมพ์เช็ค",
				content: "dlg_prt_cheque_welfare.php?val=" + val,
				id: "modal-printCheque"
			});
		}
	}
</script>

<?php
	$sql ="SELECT DISTINCT tbl_welfare.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name, sum(welfare) as welfare, cheque_no FROM tbl_welfare, expenses_lvl1, expenses_lvl2, expenses_lvl3, panngan2, panngan3  WHERE lvl1_id = '" .$_SESSION["lvl1_id"]. "' and expenses_lvl1.expenses1_id = ex1_id and expenses_lvl2.expenses2_id = ex2_id and expenses_lvl3.expenses3_id = ex3_id and panngan2.panngan2_id = tbl_welfare.panngan2_id and panngan3.panngan3_id = tbl_welfare.panngan3_id and approved_status = 'อนุมัติ'
	GROUP BY tbl_welfare.pet_id, expenses1_name, expenses2_name, expenses3_name, panngan2_name, panngan3_name
	";

	$db = new myDBS;
	$db->selectDB("account");
	$result = $db->runSQL($sql);

?>

<table id="petition" class="table table-striped table-bordered" width="100%" >
	<caption><br></caption>
	<thead>
		<tr>
			<th>#</th>
			<th>เลขที่ฎีกา</th>
			<th>แผนงาน</th>
			<th>งาน</th>
			<th>ประเภท</th>
			<th>รายการ</th>
			<th>จำนวนเงิน</th>
			<th>เลือก</th>
			
		</tr>
	</thead>
	<tbody>
	<?php
		$i=0;
		while ( $rows = $db->fetch_object ( $result )){
			$i++;
			echo "<tr>
					<td>$i</td>
					<td>$rows->pet_id</td>
					<td>$rows->panngan2_name</td>
					<td>$rows->panngan3_name</td>
					<td>$rows->expenses2_name</td>
					<td>$rows->expenses3_name</td>
					<td>". number_format($rows->welfare, 2)."</td>
			";
			if ($rows->cheque_no == "") {
				echo"<td><input type='checkbox' name='check[]' id='check_$rows->pet_id' value='$rows->pet_id' /></td>";
			} else {
				echo "<td><a href='prt_cheque_welfare.php?cheque_no=$rows->cheque_no' data-toggle='tooltip' title='พิมพ์' target='_blank'><i class='icon-print'></i></a></td>";
			}
			
			echo "
					</tr>
			";

		}
	?>
	</tbody>
</table>
<a href="javascript:printCheque()" role="button" class="btn btn-large btn-primary" target="_blank"><i class="icon-print icon-white"></i> ตกลง</a>